package com.will.txj.aj.security.controller;

import com.wf.captcha.GifCaptcha;
import com.will.txj.aj.global.utils.StringRedisUtil;
import com.will.txj.common.result.R;
import com.will.txj.common.utils.AESCrypt;
import com.will.txj.common.utils.DesUtils;
import com.will.txj.common.utils.StringUtil;
import com.will.txj.common.utils.URLUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 14:26;
 * @Description:
 */
@Slf4j
@Api(tags = "验证码控制层")
@RestController
@RequestMapping("captcha")
public class CaptchaController {
    private static final String KEY = "d084fda59e8740ce948a93d12d64e617";

    @Autowired
    private StringRedisUtil stringRedisUtil;

    @SneakyThrows
    @ApiOperation("验证信息获取验证码")
    @ApiImplicitParam(name = "info",value = "加密信息",required = true)
    @PostMapping("verify/{info}")
    public R verify(@PathVariable("info") String info, HttpSession session){
        String key = stringRedisUtil.get(session.getId());
        String[] keys = key.split("\\|");
        String params = DesUtils.strDec(info, keys[0], keys[1], keys[2]);
        return R.ok(AESCrypt.encrypt(params, KEY));
    }

    @SneakyThrows
    @ApiOperation("登录验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "width",value = "图片宽度",required = true),
            @ApiImplicitParam(name = "height",value = "图片高度",required = true)
    })
    @GetMapping("login/{info}")
    public R login(@PathVariable("info") String info, HttpServletResponse response){
        String params = AESCrypt.decrypt(info, KEY);
        Map<String, String> urlParamMap = URLUtil.getUrlParamMap(params);
        String username = urlParamMap.get("username");
        int width = Integer.parseInt(urlParamMap.get("width"));
        int height = Integer.parseInt(urlParamMap.get("height"));

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        //宽、高、位数 SpecCaptcha | GifCaptcha | ChineseCaptcha | ChineseGifCaptcha | ArithmeticCaptcha
        GifCaptcha captcha = new GifCaptcha(width, height, 5);
        String k = "login-" + username;

        String v = String.valueOf(stringRedisUtil.get(k));
        if (StringUtil.isEmptyToString(v)){
            String verify = captcha.text().toLowerCase();
            captcha.setChars(verify);
            stringRedisUtil.setEx(k,verify,1, TimeUnit.MINUTES);
            boolean out = captcha.out(response.getOutputStream());
            return out?null:R.fail();
        } else {
            captcha.setChars(v);
            boolean out = captcha.out(response.getOutputStream());
            return out?null:R.fail();
        }
    }
}
